﻿using System;
using System.Collections.Generic;
using System.Drawing;
using Emgu.CV;
using Emgu.CV.Structure;
using tessnet2;

namespace QLNXe
{
    public class ImageProcessing
    {
        int count = 1;

        /// <summary>
        /// event log
        /// </summary>
        ///
        /// <summary>
        ///
        /// </summary>
        private LicensePlates _licensePlateDetector;

        public Image<Bgr, byte> imgGray;

        public ImageProcessing()
        {
        }

        public string ProcessImage(Image<Bgr, byte> image)
        {
            string strSpotCode = string.Empty;
            string plateNumber = string.Empty;

            //count++;
            try
            {
                strSpotCode = "001";
                // license pate image list
                List<Image<Gray, Byte>> licensePlateImagesList = new List<Image<Gray, byte>>();
                strSpotCode = "002";

                // filter license pate image list
                List<Image<Gray, Byte>> filteredLicensePlateImagesList = new List<Image<Gray, byte>>();
                strSpotCode = "003";
                List<MCvBox2D> licenseBoxList = new List<MCvBox2D>();

                strSpotCode = "004";
                _licensePlateDetector = new LicensePlates();
                List<List<Word>> words = _licensePlateDetector.DetectLicensePlate(
                   image,
                   licensePlateImagesList,
                   filteredLicensePlateImagesList,
                   licenseBoxList);
                if (words.Count > 0)
                {
                    plateNumber = String.Format("{0}", String.Join(" ", words[0].ConvertAll<String>(delegate(Word w) { return w.Text; }).ToArray()));

                    plateNumber = FilterLicensePlate(plateNumber);
                }

                Point startPoint = new Point(10, 10);
                strSpotCode = "005";
                for (int i = 0; i < words.Count; i++)
                {
                    strSpotCode = "006";
                    //AddLabelAndImage(
                    //   ref startPoint,
                    //   String.Format("{0}", String.Join(" ", words[i].ConvertAll<String>(delegate(Word w) { return w.Text; }).ToArray())),
                    //   licensePlateImagesList[i].ConcateVertical(filteredLicensePlateImagesList[i]));

                    image.Draw(licenseBoxList[i], new Bgr(Color.Red), 2);
                }
            }
            catch (Exception ex)
            {
                // log file
                return null;
            }

            //  count++;
            //imageBox1.Image = image;
            //plateNumber = LicensePlate(image);

            //  if (count == 4) count = 0;
            plateNumber = FilterLicensePlate(plateNumber);
            return plateNumber;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>

        //public string ProcessImage2(Image<Bgr, byte> image)
        //{
        //    string plate = _licensePlateDetector.LicensePlate(image);
        //    return FilterLicensePlate(plate);
        //}

        /// <summary>
        ///
        /// </summary>
        /// <param name="licensePlate"></param>
        /// <returns></returns>
        private string FilterLicensePlate(string licensePlate)
        {
            licensePlate = licensePlate.Replace("-", "");
            licensePlate = licensePlate.Replace(".", "");
            licensePlate = licensePlate.Replace("_", "");
            licensePlate = licensePlate.Replace(",", "");
            licensePlate = licensePlate.Replace("|", "");
            licensePlate = licensePlate.Replace(" ", "");
            licensePlate = licensePlate.Replace("l", "");
            licensePlate = licensePlate.ToUpper();

            licensePlate = licensePlate.Replace("O", "0");
            licensePlate = licensePlate.Replace("I", "1");
            string[] temp = licensePlate.Split(',');
            if (temp.Length > 0)
            {
                licensePlate = temp[0];
            }
            if (licensePlate.Length > 10)
            {
                licensePlate = licensePlate.Substring(0, 10);
            }
            return licensePlate;
        }
    }
}